
import httplib

class BonpooPlugin:
	def __init__(self):
		# put all the links this plugins supports here - it will be used to filter supported URLs
		self.supported_sites = ['bonpoo.com']
		# the URLs we support
		self.supported_urls = ['app04.bonpoo.com/cgi-bin/download?fid=']
		self.cookies_to_block = ['bonpoo.com']

	def get(self, url):
		"""Get the file from the url"""
		parser = MetaLister()
		parser.feed(urllib2.urlopen(url).read())
		cookie_data = parse_cookie_content(parser.metas['Set-Cookie'])
		parser.close()
		name, value = cookie_data[3].split('=')
		add_manual_cookie(name, value, cookie_data[0], '/')
		url = url.replace('download', 'dnsave')

		parsed_url = httplib.urlsplit(url)
		conn = httplib.HTTPConnection(parsed_url[1])
		url_to_get = parsed_url[2] + '?' + parsed_url[3]

		conn.request("GET", url_to_get, {},
			{"User-Agent": "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)",
			"Cookie" : cookie_data[3]})
		response = conn.getresponse()
		local_file = get_filename_from_url(response.getheader('Location'))
		conn.close()

		download_file_with_retries(local_file, url)
		config.report.report_finish(local_file)

	def url_matches(self, url):
		"""Check whether we support this URL"""
		# see if the url contains a substring we are looking for
		for l in self.supported_urls:
			if url.find(l) != -1:
				return True
		return False

	def version(self):
		"""Return plugin version - when interface changes, the version changes"""
		return 1

modules = [BonpooPlugin()]
